<template>
  <div class="login">
    <div class="login-bg">
      <div class="login-bg1"/>
      <div class="login-bg2"/>
      <div class="login-bg3"/>
    </div>
    <div class="login-container">
      <div class="login-white">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
          <div class="login-white-title">
            <img src="../../assets/images/loginlogo.png">
          </div>
          <div class="title-container">
            <h3 class="title">
              登录   Welcome
            </h3>
          </div>

          <el-form-item prop="username">
            <span class="svg-container">
              <img src="../../assets/images/user.png" class="loginIcon">
            </span>
            <el-autocomplete
              v-model="loginForm.username"
              :fetch-suggestions="querySearch"
              :trigger-on-focus="false"
              class="inline-input"
              placeholder="请输入你的邮箱"
            />
          </el-form-item>

          <el-form-item>
            <span class="svg-container">
              <img src="../../assets/images/password.png" class="loginIcon">
            </span>
            <el-input
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入你的密码"
              name="password"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
          <el-form-item style="border:0">
            <div class="verifyPass">
              <span class="svg-container">
                <img src="../../assets/images/yanzhengma.png" class="loginIcon">
              </span>
              <el-input
                v-model="loginForm.captcha"
                placeholder="请输入验证码"
                name="password"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
            </div>
            <div class="verifyNum">
              <img :src="verifyIcon">
            </div>
            <img :class="reload?'rolateIcon':''" src="../../assets/images/reload.png" class="reloadIcon" @click="reloadIcon">
          </el-form-item>
          <el-button :loading="loading" type="primary" style="width:100%;margin-top:20px;" @click.native.prevent="handleLogin">
            登录
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { getUUID } from '@/api/login'
import { Message } from 'element-ui'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        captcha: '',
        t: '',
        uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false,
      redirect: undefined,
      verifyIcon: '',
      reload: false,
      emailList: ['@sunlands.com', '@qq.com', '@163.com', '@126.com', '@sina.com', '@yahoo.com']
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCap()
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    reloadIcon() {
      if (this.reload) {
        return
      }
      this.reload = true
      setTimeout(() => {
        this.getCap()
        this.reload = false
      }, 500)
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loginForm.t = Date.parse(new Date())
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: this.redirect || '/' })
          }).catch((err) => {
            Message.error(err)
            this.loading = false
            this.loginForm = {
              username: '',
              password: '',
              captcha: '',
              t: '',
              uuid: ''
            }
            this.getCap()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getCap() {
      this.loginForm.uuid = getUUID()
      this.verifyIcon = process.env.BASE_API + `captcha.jpg?uuid=${this.loginForm.uuid}`
    },
    querySearch(queryString, cb) {
      var emailList = this.emailList
      const firstStr = queryString.slice(0, queryString.indexOf('@'))
      const Laststr = queryString.slice(queryString.indexOf('@'), queryString.length)
      const arr = emailList.filter(this.createFilter(Laststr))
      const result = arr.map(item => {
        return { value: firstStr + item }
      })
      cb(result)
    },
    createFilter(queryString) {
      return (emailList) => {
        return (emailList.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">

  $bg:#fff;
  $light_gray:#000;
  $cursor: #000;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input{
      color: $cursor;
    }
  }
  /* reset element-ui css */
  .login-container {
    .el-input {
      display: inline-block;
      width: 85%;
      input {
        width: 95%;
        background: transparent;
        border: 0px;
        appearance: none;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 10px;
        line-height: normal;
        color: $light_gray;
        vertical-align: middle;
        margin-left: 6px;
        caret-color: $cursor;
        margin-top: -2px;
        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
    .el-form-item {
      border: 1px solid #DDDDDD;
      background: #fff;
      border-radius: 4px;
      color: #454545;
    }
  }
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:rebeccapurple;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-bg {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  .login-bg1 {
    width: 100%;
    height: 366px;
    background: url('../../assets/images/loginBg1.png') no-repeat center center;
  }
  .login-bg2 {
    width: 100%;
    height: 358px;
    background: url('../../assets/images/loginBg2.png') no-repeat center center;
  }
  .login-bg3 {
    width: 100%;
    height: 356px;
    background: url('../../assets/images/loginBg3.png') no-repeat center center;
  }
}
.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 35px 30px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 0 6px 0 17px;
    color: $dark_gray;
    vertical-align: middle;
    width: 16px;
    display: inline-block;
  }
  .title-container {
    position: relative;
    .title {
      font-size: 20px;
      color: #000;
      font-weight: 400;
    }
    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size:18px;
      right: 0px;
      cursor: pointer;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 0;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }
  .login-white {
    width: 350px;
    height: 420px;
    border-radius: 10px;
    background-color: white;
  }
  .login-white-title {
    img {
      width: 56px;
    }
  }
  .verifyPass {
    width: 140px;
    display: inline-block;
    vertical-align: middle;
    .el-input {
      width: 110px;
    }
    border: 1px solid #DDDDDD;
    border-radius: 4px;
  }
  .loginIcon {
    width: 16px;
    vertical-align: middle;
    margin-top: -2px;
  }
  .verifyNum {
    width: 100px;
    height: 40px;
    border: 1px solid #DDDDDD;
    display: inline-block;
    margin-left: 16px;
    border-radius: 4px;
    vertical-align: middle;
    overflow: hidden;
    img {
      width: 100%;
      height: 40px;
    }
  }
  .reloadIcon {
    width: 20px;
    vertical-align: middle;
    margin-left: 6px;
    cursor: pointer;
  }
  .rolateIcon {
    transform: rotate(1080deg);
    transition:transform 1s linear;
  }
  .inline-input {
    width: 240px
  }
}
</style>
